package com.lhd.controller;

import cn.hutool.core.bean.BeanUtil;
import com.lhd.component.RedisComponent;
import com.lhd.entity.constants.Constants;
import com.lhd.entity.dto.*;
import com.lhd.entity.enums.ActivityTypeRedisKeyEnum;
import com.lhd.entity.enums.RecordStatusEnum;
import com.lhd.entity.po.Record;
import com.lhd.entity.query.RecordQuery;
import com.lhd.entity.vo.PaginationResultVO;
import com.lhd.entity.vo.ResponseVO;
import com.lhd.service.RecordService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: liuhd
 * @Date: 2025/03/10
 * @Description: Record控制器
 */
@RestController
@RequestMapping("/record")
public class RecordController extends ABaseController{

	@Resource
	private RecordService recordService;
	@Resource
	private RedisComponent redisComponent;
	@PutMapping("/submit")
	public ResponseVO submit(@RequestBody SubmitDto submitDto){

		UserDto userInfoToken = getUserInfoToken();
		Integer stuId = userInfoToken.getId();
		recordService.submit(stuId, submitDto);
		return getSuccessResponseVO(null);
	}

	/**
	 * @description: 根据aid获取签到记录
	 * @param aid
	 * @return com.lhd.entity.vo.ResponseVO
	 * @author liuhd
	 * 2025/4/30 13:43
	 */
	@GetMapping("/{aid}")
	public ResponseVO getRecordByAid(@PathVariable Integer aid, @RequestParam(name = "pageSize",defaultValue = "15") Integer pageSize,@RequestParam(name = "pageNo",defaultValue = "1") Integer pageNo){
		RecordQuery recordQuery = new RecordQuery();
		recordQuery.setPageNo(Long.valueOf(pageNo));
		recordQuery.setPageSize(Long.valueOf(pageSize));
		recordQuery.setAid(aid);
		recordQuery.setNeedStudentInfo(true);
		PaginationResultVO<Record> resultVO = recordService.findListByPage(recordQuery);
		return getSuccessResponseVO(resultVO);
	}

	/**
	 * @description: 根据stuId获取签到记录
	 * @param pageSize
	 * @param pageNo
	 * @return com.lhd.entity.vo.ResponseVO
	 * @author liuhd
	 * 2025/4/30 14:22
	 */
	@GetMapping
	public ResponseVO getRecordByUid(@RequestParam(name = "pageSize",defaultValue = "15") Integer pageSize,@RequestParam(name = "pageNo",defaultValue = "1") Integer pageNo){
		Integer stuId = getUserInfoToken().getId();
		RecordQuery recordQuery = new RecordQuery();
		recordQuery.setPageNo(Long.valueOf(pageNo));
		recordQuery.setPageSize(Long.valueOf(pageSize));
		recordQuery.setStuId(stuId);
		recordQuery.setNeedDetailInfo(true);
		PaginationResultVO<Record> resultVO = recordService.findListByPage(recordQuery);
		return getSuccessResponseVO(resultVO);
	}

	/**
	 * @description: 获取待办签到
	 * @param pageSize
	 * @param pageNo
	 * @return com.lhd.entity.vo.ResponseVO
	 * @author liuhd
	 * 2025/4/30 15:55
	 */
	@GetMapping("/getTodoRecord")
	public ResponseVO getTodoRecord(@RequestParam(name = "pageSize",defaultValue = "15") Integer pageSize,@RequestParam(name = "pageNo",defaultValue = "1") Integer pageNo){
		Integer stuId = getUserInfoToken().getId();
		RecordQuery recordQuery = new RecordQuery();
		// 找到正在进行的活动
		recordQuery.setStuId(stuId);
		recordQuery.setStatus(RecordStatusEnum.TODO_LEAVE.getCode());
		recordQuery.setPageSize(Long.valueOf(pageSize));
		recordQuery.setPageNo(Long.valueOf(pageNo));
		recordQuery.setGetTodoRecord(true);
		PaginationResultVO<Record> resultVO = recordService.findListByPage(recordQuery);
		return getSuccessResponseVO(resultVO);
	}

	@PutMapping("/modifyRecordStatus/{cid}")
	public ResponseVO getTodoRecord(@PathVariable(value = "cid") Integer cid,@RequestBody StatusDto statusDto){
		Record record = new Record();
		record.setStatus(statusDto.getStatus());
		recordService.updateRecordById(record,cid);
		return getSuccessResponseVO(null);
	}

	@GetMapping("/rid/{rid}")
	public ResponseVO getById(@PathVariable(value = "rid") Integer rid){
		Record record = recordService.getRecordById(rid);
		String key = Constants.REDIS_KEY + ActivityTypeRedisKeyEnum.LOCATION_CHECK_IN.getRedisKey() + ":" + record.getAid();
		Map commonMap = redisComponent.getCommonMap(key);
		AdditionInfoDto additionInfoDto = new AdditionInfoDto();

		LocationDto locationDto = new LocationDto();

		HashMap locationDtoMap = (HashMap) commonMap.get("locationDto");

		BeanUtil.fillBeanWithMap(locationDtoMap,locationDto,false);

		additionInfoDto.setLocationDto(locationDto);

		record.setAdditionInfoDto(additionInfoDto);

		return getSuccessResponseVO(record);
	}
}